package com.cn.algorithm.two.line;

import java.util.Arrays;

public class CountSort {


    public static void main(String[] args) {
        int[] arr = {9, 7, 4, 2, 5, 3, 8};
        sort(arr);
        System.out.println(Arrays.toString(arr));
    }
    public static void sort(int[] arr) {
        if (arr == null || arr.length == 0) {
            return;
        }
        int max = arr[0];
        int min = max;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > max ) {
                max = arr[i];
            } else if (arr[i] < min) {
                 min = arr[i];
            }
        }
        int[] tmp = new int[max + 1];
        for (int i = 0; i < arr.length; i++) {
            tmp[arr[i]]++;
        }
        int index = 0;
        for (int i = min; i < tmp.length; i++) {
            if (tmp[i] > 0) {
                arr[index++] = i;
            }
        }
    }
}
